Makes a new LIST of just the values of target that the script test says yes to, in their original order. test is the NAME of one of your scripts; it takes one argument and gives back true or false. The answer is a list, not an array, because how many pass is only known while the game runs. target is not changed.
array_filter(target, test) -> list<T>| Name | Type |
|---|---|
target | array |
test | script |
list<T>
Makes a new list holding only the values of target, an array, that a script says yes to. The script is asked about each value in turn. It answers true to keep the value and false to leave it out. The values that are kept stay in the order they had. target itself is not changed.
var high = array_filter(scores, is_high);If scores holds 30, 80, 55 and 10 and is_high says yes to 50 or more, high holds 80 and 55.
The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that answers whether a score is high:
// scripts/is_high.gbml
function is_high(score: int): bool {
return score >= 50;
}The script has to follow four rules, and each one is checked before the game runs:
score: int. An array of whole numbers fits a parameter that wants decimals, but an array of decimals does not fit one that wants whole numbers. A script that is only used here can leave the type out: it takes the kind of the values of the array. Write it when you want it checked.true or false. Write : bool after the parameters and return true; or return false; in the script. A script that gives back a number, or nothing, is refused.function (n) { return n > 5; } in the middle of the line works as well: it takes the kind of the values of the array for its parameter, and the same rules hold for it.You do not have to make a script file for a short job. Write the function right where the name would go:
var high = array_filter(nums, function (n) { return n >= 50; });It is the script, only without a name. Its one parameter takes the kind of the values of the array, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.
The answer is a list, not an array. How many values pass is only found out while the game runs, and an array's size is fixed when the game is built. A list is the container made for exactly this: it has a fixed capacity and a length that goes up and down. So you ask the answer how many passed with list_length, and you can go on to add to it or take from it like any list.
The new list has room for as many values as the array has places, because the most that could pass is every value. An array of 40 places makes a list with room for 40, even if only 3 values pass. So a filter costs a whole list's worth of memory. If the array is a variable of an object, every instance of the object carries its own.
For the same reason a filter can only be written in one place: the whole right-hand side of the line that makes the variable, like var high = array_filter(scores, is_high);, or high = array_filter(scores, is_high); in an object's Create event. It cannot go inside another call or a sum, on a line by itself, over a variable that already exists, or in a later event such as Step.
It also needs to know how big the array you filter is, so that has to be a plain variable name whose size is known. A script's parameter could be any size, so it is refused. A filter cannot be given a size of its own either.
| You do this | What happens |
|---|---|
| the script says no to every value | the new list is empty |
| the script says yes to every value | the new list holds all of them, in the same order |
| an array of text, structs or references | works. Nothing is compared, the script decides |
give a script that is not a script's name, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or false | refused when checked, and the message says which |
| give it a list or a grid | refused when checked, and the message names the function for that kind, like list_filter |
| use it where a value is needed, other than as the whole right-hand side of the line that makes a variable | refused when checked |
One call of your script for each place the array has, and a whole list of memory with room for as many values as the array has places. The array is not changed.
var scores = array_create(4, 0);
scores[0] = 30;
scores[1] = 80;
scores[2] = 55;
scores[3] = 10;
var high = array_filter(scores, is_high); // a list holding 80, 55
var how_many = list_length(high); // 2A fixed-size vector with the same N as the array, filled by a loop that adds each value the script accepts. The answer is a LIST, not an array: how many items pass is only found out while the game runs, and an array's size is fixed when the game is built, whereas a list has a fixed capacity and a length that changes. Its capacity is the original's (the most that could pass is all of them), so it is a second whole container of memory: as many places as the original has room for, not just how many pass. Like a copy, it has to be the whole right-hand side of the line that makes the variable, the original has to be a variable whose size is known (a script's parameter could be any size), and it cannot be given a size of its own. The script is called once for each item, in order, and the items it says yes to are added to the answer in the same order; the original is not changed. The script has to give back true or false, take exactly one argument whose type accepts the items, and be the NAME of one of your scripts: a function written out in the middle of the line is not turned into game code yet, so it is refused instead of doing nothing. O(n) calls of the script, and no timing was measured.